package com.example.front.mapper;


import com.example.common.dto.Pagination;
import com.example.front.dto.BoxCreateDto;
import com.example.front.dto.MaintenanceDto;
import com.example.front.entity.Category;
import com.example.front.entity.Equipment;
import com.example.front.entity.Maintenance;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MaintenanceMapper {

    @Select("<script>" +
            "SELECT * FROM `maintenance` " +
            "<where>" +
            "<if test='maintenance.id!=null'> AND `id`=#{maintenance.id}</if>" +
            "<if test='maintenance.name!=null'> AND `name` LIKE CONCAT('%',#{maintenance.name},'%') </if>" +
            "<if test='maintenance.lineId!=null'> AND `line_id`=#{maintenance.lineId} </if>" +
            " AND `tenant_id`=#{maintenance.tenantId}"+
            " AND `status`!=" + User.STATUS_DELETE +
            "</where>"+
            " ORDER BY `id` LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Maintenance> findAll(@Param("maintenance") Maintenance maintenance, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT count(*) FROM `maintenance` " +
            "<where>" +
            "<if test='maintenance.id!=null'> AND `id`=#{maintenance.id}</if>" +
            "<if test='maintenance.name!=null'> AND `name` LIKE CONCAT('%',#{maintenance.name},'%') </if>" +
            "<if test='maintenance.lineId!=null'> AND `line_id`=#{maintenance.lineId} </if>" +
            " AND `tenant_id`=#{maintenance.tenantId}"+
            " AND `status`!=" + User.STATUS_DELETE +
            "</where>"+
            "</script>")
    long count(@Param("maintenance") Maintenance maintenance);

    @Update("<script>" +
            "UPDATE `maintenance` " +
            "<set>" +
            "<if test='maintenanceDto.name!=null'>`name`= #{maintenanceDto.name},</if>" +
            "<if test='maintenanceDto.categoryId!=null'>`category_id`= #{maintenanceDto.categoryId}, </if>" +
            "<if test='maintenanceDto.lineId!=null'>`line_id`= #{maintenanceDto.lineId},</if>" +
            "</set>" +
            " WHERE `id`=#{maintenanceDto.id}" +
            " AND `tenant_id`=#{maintenanceDto.tenantId}"+
            "</script>")
    int update(@Param("maintenanceDto") MaintenanceDto maintenanceDto);

    @Select("<script>" +
            "SELECT * FROM `maintenance` " +
            "WHERE `id` = #{id}" +
            " AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    Maintenance findById(@Param("id") Long id);


    @Update("<script>" +
            "UPDATE `maintenance` set status = " + User.STATUS_DELETE +
            " WHERE `id`=#{id} " +
            " AND `tenant_id`=#{tenantId}"+
            "</script>")
    int deletion(@Param("id") Long id,@Param("tenantId") Long tenantId);


    @Select("<script>" +
            "SELECT * FROM `maintenance` " +
            "<where>" +
            "<if test='maintenance.lineId!=null'> AND FIND_IN_SET(#{maintenance.lineId},`line_id`) </if>" +
            "AND `status`!=" + User.STATUS_DELETE +
            "</where> "+
            "</script>")
    Maintenance selectOne(@Param("maintenance") Maintenance maintenance);


    @Insert("INSERT INTO `maintenance`" +
            " (`name`,`line_id`,`category_id`,`tenant_id`) VALUES" +
            " (#{name},#{lineId}, #{categoryId},#{tenantId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Long create(MaintenanceDto maintenanceDto);

}
